package com.gc.logger.printer

import android.content.Context
import android.util.Log
import com.gc.logger.LogUtils
import com.gc.logger.entity.LogEntity
import com.gc.logger.interfaces.ILogPrinter

class AndroidLogPrinter : ILogPrinter {
    override var logType: LogUtils.LogType
        get() = LogUtils.LogType.System
        set(value) {}

    override fun log(context: Context, logEntity: LogEntity) {
        var tag = "${logEntity.defTag}:${logEntity.tag}"
        if (logEntity.message.length < LogUtils.MAX_LOG_LENGTH) {
            Log.println(logEntity.priority, tag, logEntity.message)
            return
        }
        var i = 0
        val length = logEntity.message.length
        while (i < length) {
            var newline = logEntity.message.indexOf('\n', i)
            newline = if (newline != -1) newline else length
            do {
                val end = Math.min(newline, i + LogUtils.MAX_LOG_LENGTH)
                val part = logEntity.message.substring(i, end)
                Log.println(logEntity.priority, tag, part)
                i = end
            } while (i < newline)
            i++
        }
    }
}